package com.hmall.item.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hmall.item.mapper.ItemMapper;
import com.hmall.item.pojo.Item;
import com.hmall.item.pojo.PageDTO;
import com.hmall.item.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ItemServiceImpl extends ServiceImpl<ItemMapper, Item> implements ItemService {

    @Autowired
    private ItemMapper itemMapper;


    /**
     * 分页查询商品
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageDTO<Item> getPage(Integer page, Integer size) {

        PageHelper.startPage(page,size);
        List<Item> items = itemMapper.selectList(null);
        PageInfo<Item> pageInfo = new PageInfo<>(items);
        PageDTO<Item> pageDTO = new PageDTO<>();
        pageDTO.setTotal(pageInfo.getTotal());
        pageDTO.setList(pageInfo.getList());

        return pageDTO;
    }

    /**
     * 新增商品
     * @param item
     */
    @Override
    public void insertItem(Item item) {
        item.setStatus(1);
        item.setCreateTime(new Date());
        item.setUpdateTime(new Date());
        itemMapper.insert(item);
    }
    /**
     * 商品上架、下架
     * @param id
     * @param status
     */
    @Override
    public void updateStatusById(Long id, Integer status) {
        LambdaUpdateWrapper<Item> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(Item::getStatus,status).eq(Item::getId,id);
        itemMapper.update(null,updateWrapper);
    }

    /**
     * 修改商品
     * @param item
     */
    @Override
    public void updateItem(Item item) {
        LambdaUpdateWrapper<Item> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(Item::getStatus,item.getStatus())
                .set(Item::getCategory,item.getCategory())
                .set(Item::getBrand,item.getBrand())
                .set(Item::getPrice,item.getPrice())
                .set(Item::getStock,item.getStock())
                .set(Item::getSpec,item.getSpec())
                .set(Item::getImage,item.getImage())
                .set(Item::getIsAD,item.getIsAD())
                .eq(Item::getId,item.getId());
        itemMapper.update(null,updateWrapper);
    }


}
